package com.baiku.sanhos.controller;
import com.baiku.sanhos.mapper.AdminMapper;
import com.baiku.sanhos.pojo.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

@Component
public class AdminDataLoaderController {
    @Value("${admin.username}")
    private String adminUsername;

    @Value("${admin.password}")
    private String adminPassword;

    @Value("${admin.phone}")
    private Long adminPhone;

    @Value("${admin.flag}")
    private Integer adminFlag;

    @Autowired
    private AdminMapper adminRepository;

    @PostConstruct
    public void loadDataToDatabase() {
        // 创建管理员实体对象并保存到数据库
        Admin admin = new Admin();
        admin.setUsername(adminUsername);

        //密码加密
        BCryptPasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String password=passwordEncoder.encode(adminPassword);
        admin.setPassword(password);
        admin.setPhone(adminPhone);
        admin.setFlag(adminFlag);
        adminRepository.saveInsert(admin);
    }
}
